/*
============================================================================
 Name        : nmsJpegDefine.h
 Author      : Jian
 Version     :
 Copyright   : base on http://ce.sharif.edu/~ahmadinejad/jpeg/
 Description : nmsJpegDefine.h - header file
============================================================================
*/

#ifndef NMSJPEGDEFINE_H
#define NMSJPEGDEFINE_H


typedef unsigned int    HFILE;
#define UINT32          unsigned int
#define UINT16          unsigned short
#define WCHAR           unsigned short
#define UINT8           unsigned char

#define NMS_FS_CREATE          0x00010000L
#define NMS_FS_CREATE_ALWAYS   0x00020000L
#define NMS_FS_READ            0x00000100L
#define NMS_FS_READ_WRITE      0x00000000L
#define NMS_FS_FILE_BEGIN      0
#define NMS_FS_FILE_CURRENT    1
#define NMS_FS_FILE_END        2

#ifndef NULL
#define NULL 0
#endif

#define nmsMemSet     memset
#define nmsMemCpy     memcpy

void   *nmsMallocL(UINT32 size);
void    nmsFreeL(void* pMem);

HFILE   nmsFsOpenW(WCHAR *name, UINT32 flag);
int     nmsFsRead(HFILE hFile, void *pData, int nLen, int *Read);
int     nmsFsWrite(HFILE hFile, void *pData, int nLen, int *Written);
int     nmsFsSeek(HFILE hFile, int offset, int Whence);
int     nmsFsFlush(HFILE hFile) ;
int     nmsFsTruncate(HFILE  hFile, int size);
void    nmsFsClose(HFILE hFile);
int     nmsFsCopyW(WCHAR *srcName, WCHAR *desName) ;
int     nmsFsGetFileSizeWithNameW(WCHAR *filePath, int* pSize);
int     nmsStrCaseCmp(char *src, char *des);
UINT32  nmsGetSystemTime();
int     nmsUcs2StrCaseCmp(unsigned short *s1, unsigned short *s2);
int     nmsUcs2Strlen(unsigned short *start);
void    nmsUcs2Cpy (void *dest, void *src);

#include <string.h>

void _nprintf(char *format,...) ;

#define nprintf _nprintf
// #define nprintf(...)

/*
* Jpeg custom definition
*/

#define __NMS_JPEG_DEBUG
#define __NMS_JPEG_GOOD_QUALITY
// #define __NMS_JPEG_DCT_USE_FLOAT

typedef signed char  INT8 ;  /* For the damn Symbian S60 3rd Fp1 GCCE compiler, the char default is [unsigned char]. damn to waste so many time */

#define __NMS_MIN_SCALE  7   /* TODO: Now for implement limited, the min scale is 7; 16 * 6 < 100, 16 * 7 > 100 */

#ifdef __arm
#define __NMS_INLINE  __inline
#else
#define __NMS_INLINE  inline
#endif

typedef int    nmsMatrix[64] ;
typedef UINT8  nmsJpegDqt[64] ;
typedef UINT32 nmsRgbMatrix[4][64] ; /* index 0: top-left, 1: top-right, 2: bottom-left, 3: bottom-right */

#define __getR(rgb) (UINT8)((UINT32)(rgb) >> 16)
#define __getG(rgb) (UINT8)((UINT32)(rgb) >> 8)
#define __getB(rgb) (UINT8)(rgb)

#define __NMS_MAX_DQT_INDEX 3
#define __NMS_MAX_DHT_INDEX 3

enum __nms_support_sample_type {
    __NMS_JPEG_GREY = 1,
    __NMS_JPEG_YUV  = 3
};

enum __nms_comp_id {
    __NMC_JPEG_COMP_ID_NONE = 0,
    __NMC_JPEG_COMP_ID_Y,
    __NMC_JPEG_COMP_ID_CB,
    __NMC_JPEG_COMP_ID_CR
};

int    nmsFsTell(HFILE hFile);


#define __Log_File_Line     nprintf("Assert in %s, line = %d", __FILE__, __LINE__)

#ifdef __NMS_JPEG_DEBUG
int nmsRandom() ;
void nmsAssertPainc(const char* fileName, int line) ;
#define __Panic_File_Line   nmsAssertPainc(__FILE__, __LINE__)
#else
#define __Panic_File_Line   {}
#endif

#define __NMS_ASSERT(c)              do { if(!((c))) { __Log_File_Line; __Panic_File_Line; } } while(0)
#define __NMS_ASSERT_DEFENSE(c,d)    do { if(!((c))) { __Log_File_Line; __Panic_File_Line; {d;}; } } while(0)


#ifdef __NMS_JPEG_DEBUG
void* nmsDebugMalloc(UINT32 size, const char* fileName, int line) ;
void  nmsDebugFree(void* ptr) ;
#define nmsJpegMalloc(s)  nmsDebugMalloc(s, __FILE__, __LINE__)
#define nmsJpegFree(p)    nmsDebugFree(p) ;
#else
#define nmsJpegMalloc(s)  nmsMallocL(s)
#define nmsJpegFree(p)    nmsFreeL(p)
#endif



#endif  /* NMSJPEGDEFINE_H */

/* End of File */
